package 案例.生产消费;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 演示效果
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/7/6 下午8:26
 */
public class Play {

    public static void main(String[] args) {
        Food food = new Food();
        Cooker p = new Cooker(food);
        Waitter c = new Waitter(food);
        /**
         * 传统方式
         * 解释一下，第一次为什么会有null菜品出现
         */
//        new Thread(p).start();
//        new Thread(p).start();
//        new Thread(c).start();
//        new Thread(c).start();

        ThreadPoolExecutor tpe = new ThreadPoolExecutor(3, 5, 5,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(20), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t1 = new Thread(r);
                t1.setName("ss");
                return t1;
            }
        }, new ThreadPoolExecutor.AbortPolicy());

        /**
         * 添加线程进池，处理列队
         */
        tpe.execute(p);
        tpe.execute(c);

        tpe.shutdown();


    }

}
